//
//  WHWproxyKitModel.m
//  WHPersonalData
//
//  Created by shengmeng chen on 2021/7/20.
//  Copyright © 2021 mac. All rights reserved.

#import "WHWproxyKitModel.h"
#import <XmlHelper/XmlHelper.h>
#import "WHFileManager.h"
#import "NSString+Base64.h"

@implementation WHWproxyKitErrorModel

@end

@implementation WHWproxyKitProcessModel

@end

@implementation WHWproxyKitSuccessModel

@end

@implementation WHWproxyKitModel

+(WHWproxyKitErrorModel*)wHWproxyKitErrorModelWithResult:(NSString *)result
{
    if (result.length>0)
    {
        XmlHelper*xmlHelper =[XmlHelper new];
        if ([xmlHelper parse:result] ||[xmlHelper seek:@"info"])
        {
            WHWproxyKitErrorModel*errorModel = [WHWproxyKitErrorModel new];
            errorModel.error_code = [xmlHelper childText:@"error_code"];
            errorModel.error_msg  = [xmlHelper childText:@"error_msg"];
            return errorModel;
        }
    }
    return nil;
}

+(WHWproxyKitProcessModel*)wHWproxyKitProcessModelWithResult:(NSString *)result
{
    if (result.length>0)
    {
        XmlHelper *xmlHelper = [XmlHelper xmlHelperWithEncoding:NSUTF8StringEncoding];
        if ([xmlHelper parse:result] && [xmlHelper seek:@"info"])
        {
            WHWproxyKitProcessModel*processMode = [WHWproxyKitProcessModel new];
            processMode.doing_file = [xmlHelper childText:@"doing_file"];
            processMode.doing_pos  = [xmlHelper childText:@"doing_pos"];
            processMode.doing_size = [xmlHelper childText:@"doing_size"];
            processMode.total_pos  = [xmlHelper childText:@"total_pos"];
            processMode.total_size = [xmlHelper childText:@"total_size"];
            return processMode;
        }
    }
    return nil;
}

+(NSMutableArray<WHWproxyKitSuccessModel*>*)wHWproxyKitSuccessModelWithResult:(NSString *)result
{
    if (result.length>0)
    {
         NSMutableArray*successModels =[NSMutableArray new];
         XmlHelper *xmlHelper = [XmlHelper xmlHelperWithEncoding:NSUTF8StringEncoding];
        if ([xmlHelper parse:result] && [xmlHelper seek:@"manifest"])
        {
            if ([xmlHelper child:@"item"])
            {
                do
                {
                    WHWproxyKitSuccessModel*successModel = [WHWproxyKitSuccessModel new];
                    successModel.file_path  = [xmlHelper attrStringValue:@"file"];
                    successModel.file_size  = [xmlHelper attrStringValue:@"size"];
                    successModel.file_name  = [xmlHelper text];
                    successModel.file_rid   = [xmlHelper attrStringValue:@"rid"];
                    [successModels addObject:successModel];
                }
                while ([xmlHelper next:@"item"]);
            }
        }
     return successModels;
    }
    return nil;
}

+(NSString*)getTxtFilePathWithArray:(NSMutableArray<WHWproxyKitSuccessModel*>*)successModels
{
    NSString*filePath ;
    for(int i = 0;i <successModels.count;i++)
    {
        WHWproxyKitSuccessModel*successModel = successModels[i];
        //获得文件后缀
        NSString *suffix    = [successModel.file_path pathExtension];       //txt
        if ([suffix isEqualToString:@"txt"])
        {
            filePath = successModel.file_path;
        }
    }
    return  filePath;
}
+(NSString *)readJsonTxtFileWithPath:(NSString*)path
{
    NSString *json ;
    if (path.length>0)
    {
         if ([WHFileManager isExistsAtPath:path])
        {
            NSStringEncoding gbk=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
            NSFileManager*fileManager  =[NSFileManager new];
            NSData *data =  [fileManager contentsAtPath:path];
            json = [[NSString alloc] initWithData:data  encoding:gbk];
        }
    }
    return json;
}

+(NSString *)readJsonTxtContentWithArray:(NSMutableArray<WHWproxyKitSuccessModel*>*)successModels
{
    NSString *path = [WHWproxyKitModel getTxtFilePathWithArray:successModels];
    if (path.length>0) {
        NSString *json = [WHWproxyKitModel readJsonTxtFileWithPath:path];
        return  json;
    }else
    {
        return  nil;
    }
}

@end





